﻿#include "stdafx.h"
#include "MainWindow.h"
#include "FrameInclude.h"
#include "PlanInclude.h"

static int iTimeCount = 0;

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	initMainWindow();
	pPowerCutManager = NULL;
	pWorkHolidayInfo = NULL;
}

MainWindow::~MainWindow()
{
	
}

/***********************************************************************************************************************************/

/***********************************************************************************************************************************/
void MainWindow::initMainWindow()
{
	pFrameHelper = new FrameHelper(this);
	pFrameHelper->addEdgeWidget(this);
	pFrameHelper->addDragWidget(this);
	pFrameHelper->setCloseButton(ui.titleBar->getCloseBtn());
	pFrameHelper->setMinimizedButton(ui.titleBar->getMinBtn());
	pFrameHelper->setMaxRestoreButton(ui.titleBar->getMaxBtn());

	/*Time Index User*/
	displayTime();
	pTimer = new QTimer;
	connect(pTimer, SIGNAL(timeout()), this, SLOT(onTimer()));
	pTimer->start(500);

	ui.labCurUserName->setText(tr("guanliyuan"));
	ui.labCurUserDept->setText(tr("xitongguanlibu"));

	//connect(ui.tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_close(int)));
	//connect(ui.pageIndex, SIGNAL(sheetListDoubleClicked(int, const QString &)), this, SLOT(on_sheetListView_doubleClicked(int, const QString &)));
}

void MainWindow::displayTime()
{
	iTimeCount++;
	if (iTimeCount >= 60)
	{
		iTimeCount = 0;
	}
	const QDateTime & time = QDateTime::currentDateTime();
	const QString & strTime = time.toString("hh:mm");
	const QString & strNextTime = time.toString("hh mm");

	if (iTimeCount % 2 == 0)
	{
		ui.lcdTime->display(strTime);
	} 
	else
	{
		ui.lcdTime->display(strNextTime);
	}
}

void MainWindow::setWidgetStatus(QToolButton * btn, QWidget * w)
{
	if (btn->isChecked())
	{
		w->setVisible(true);
		btn->setArrowType(Qt::DownArrow);
	}
	else
	{
		w->setVisible(false);
		btn->setArrowType(Qt::RightArrow);
	}
}

void MainWindow::deleteTabWidget(QWidget * widget)
{
	
}

void MainWindow::setTabWidget(QWidget * widget, const QString & name)
{
	int pos = ui.tabWidget->indexOf(widget);
	if (pos == -1)
	{
		pos = ui.tabWidget->addTab(widget, name);
		ui.tabWidget->setTabToolTip(pos, name);
	}

	ui.tabWidget->setCurrentIndex(pos);
	ui.stackedWidget->setCurrentIndex(1);
}

//============================================================================================================================================
void MainWindow::onTimer()
{
	displayTime();
}

void MainWindow::on_listPowerCutPlan_clicked(const QModelIndex & index)
{
	switch (index.row())
	{
	case 0:
		if (pPowerCutManager == NULL)
		{
			pPowerCutManager = getPowerCutList();
		}
		setTabWidget(pPowerCutManager, tr("PowerCutManager"));
		break;

	case 1:

		break;

	case 2:
		if (pWorkHolidayInfo == NULL)
		{
			pWorkHolidayInfo = getWorkHolidayInfo();
		}
		setTabWidget(pWorkHolidayInfo, tr("WorkHolidayInfo"));
		break;

	}
}







